package it.greg.findparty.android.ui;

import it.greg.findparty.R;
import it.greg.findparty.android.util.SharedObjects;
import it.greg.findparty.model.Evento;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends Activity {

	private ArrayList<String> arrayEventi;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_events_list);
		try {
			Intent intent = getIntent();
			this.arrayEventi = intent.getStringArrayListExtra("arrayEventi");
		} catch(Exception e) {
			Toast.makeText(this, "Error:\n"+e.getMessage(), Toast.LENGTH_LONG).show();
		}
		ListView lstEventi = (ListView) findViewById(R.id.lstEvents);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.row, R.id.textViewList, this.arrayEventi);
		lstEventi.setAdapter(adapter);
		lstEventi.setTextFilterEnabled(true);
		lstEventi.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				loadDetailsPage(position);
			}
		});
		Toast.makeText(this, "Trovati "+this.arrayEventi.size()+" eventi!", Toast.LENGTH_SHORT).show();
	}
	
	private void loadDetailsPage(int eventPosition){
		Iterator<Evento> it = SharedObjects.mapEvents.keySet().iterator();
		for (int x=eventPosition;x>0;x--)
			it.next();
		Evento evento = it.next();
		Intent intent = new Intent(this, EventDetailsActivity.class);
		intent.putExtra("titolo", evento.getTitolo());
		intent.putExtra("descrizione", evento.getDescrizione());
		intent.putExtra("data", evento.getData());
		intent.putExtra("ora", evento.getOra());
		intent.putExtra("indirizzo", evento.getIndirizzo());
		intent.putExtra("fbpage", "https://m.facebook.com/search/?query="+URLEncoder.encode(evento.getTitolo()));
		startActivity(intent);
	}

}
